<template>
  <div class="container">
    <!-- 查询功能 -->
    <el-card class="search-card">
    <div class="search-container">
      <el-input
        v-model="searchQuery"
        placeholder="请输入查询内容"
        class="search-input"
      ></el-input>
      <el-button @click="handleSearch(1)" type="primary">查询</el-button>
      <el-button @click="customizeDialogVisible = true" type="primary">个性化</el-button>
      <el-button type="danger" @click="handleDelete">批量删除</el-button>
    </div>
    </el-card>

    <!-- 表格展示 -->
    <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange" class="custom-table">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="序号" width="90" align="center" sortable></el-table-column>
      <el-table-column v-if="isColumnVisible('company')" prop="company" label="公司" width="150"></el-table-column>
      <el-table-column v-if="isColumnVisible('jobTitle')" prop="jobTitle" label="岗位名称" width="150"></el-table-column>
      <el-table-column v-if="isColumnVisible('website')" prop="website" label="官网链接" width="200">
        <template slot-scope="scope">
          <el-link :href="formatUrl(scope.row.website)" target="_blank">{{ scope.row.website }}</el-link>
        </template>
      </el-table-column>
      <el-table-column v-for="column in filteredOptionalColumns" :key="column.value" :prop="column.value" :label="column.label"></el-table-column>
      <el-table-column prop="notes" label="备注" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="showNotes(scope.row.notes)">详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      background
      @current-change="handlePageChange"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="total, prev, pager, next, jumper"
      :total="total"
      class="custom-pagination"
    ></el-pagination>

    <!-- 备注详情卡片 -->
    <el-dialog :visible.sync="dialogVisible" title="备注详情">
      <p>{{ selectedNotes }}</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

    <!-- 编辑弹窗 -->
    <el-dialog :visible.sync="editDialogVisible" title="编辑信息">
      <el-form :model="editForm">
        <el-form-item label="公司" :rules="[{ required: true, message: '公司名称为必填项', trigger: 'blur' }]">
          <el-input v-model="editForm.company"></el-input>
        </el-form-item>
        <el-form-item label="岗位名称" :rules="[{ required: true, message: '岗位名称为必填项', trigger: 'blur' }]">
          <el-input v-model="editForm.jobTitle"></el-input>
        </el-form-item>
        <el-form-item label="官网链接">
          <el-input v-model="editForm.website"></el-input>
        </el-form-item>
        <el-form-item label="偏爱优先级">
          <el-select v-model="editForm.priority" placeholder="请选择">
            <el-option label="高" value="高"></el-option>
            <el-option label="中" value="中"></el-option>
            <el-option label="低" value="低"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流程进展">
          <el-input v-model="editForm.progress"></el-input>
        </el-form-item>
        <el-form-item label="Base">
          <el-input v-model="editForm.base"></el-input>
        </el-form-item>
        <el-form-item label="规模">
          <el-input v-model="editForm.size"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-input v-model="editForm.type"></el-input>
        </el-form-item>
        <el-form-item label="行业">
          <el-input v-model="editForm.industry"></el-input>
        </el-form-item>
        <el-form-item label="笔试时间">
          <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" v-model="editForm.examTime" type="datetime" placeholder="选择日期和时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="面试时间">
          <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" v-model="editForm.interviewTime" type="datetime" placeholder="选择日期和时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="面试轮次">
          <el-input v-model="editForm.interviewRounds"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="editForm.notes"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleEditSubmit">提交</el-button>
      </span>
    </el-dialog>

    <!-- 个性化选项卡片 -->
    <el-dialog :visible.sync="customizeDialogVisible" title="选择展示属性">
      <el-checkbox-group v-model="selectedColumns">
        <el-checkbox v-for="option in columnOptions" :key="option.value" :label="option.value">
          {{ option.label }}
        </el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="customizeDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      selectedColumns: ['company', 'jobTitle', 'website', 'notes'],
      columnOptions: [
        { label: '公司', value: 'company' },
        { label: '岗位名称', value: 'jobTitle' },
        { label: '官网链接', value: 'website' },
        { label: '偏爱优先级', value: 'priority' },
        { label: '流程进展', value: 'progress' },
        { label: 'Base', value: 'base' },
        { label: '规模', value: 'size' },
        { label: '类型', value: 'type' },
        { label: '行业', value: 'industry' },
        { label: '笔试时间', value: 'examTime' },
        { label: '面试时间', value: 'interviewTime' },
        { label: '面试轮次', value: 'interviewRounds' },
        { label: '备注', value: 'notes' }
      ],
      tableData: [{company:'嘉苇', jobTitle:'游戏客户端', website:'www.baidu.com',
      priority:'高',progress:'二面',base:'pku',size:'中',type:'女',industry:'五',
      examTime:'2024-01-01',interviewRounds:'2025-01-01',interviewRounds:2,notes:'skdjslkdjlkasd'
       }],
      selectedNotes: '',
      dialogVisible: false,
      editDialogVisible: false,
      customizeDialogVisible: false,
      editForm: {},
      editId:'',
      selectedRows: [],
      optionalColumns: [
        { label: '偏爱优先级', value: 'priority' },
        { label: '流程进展', value: 'progress' },
        { label: 'Base', value: 'base' },
        { label: '规模', value: 'size' },
        { label: '类型', value: 'type' },
        { label: '行业', value: 'industry' },
        { label: '笔试时间', value: 'examTime' },
        { label: '面试时间', value: 'interviewTime' },
        { label: '面试轮次', value: 'interviewRounds' }
      ],
      currentPage: 1,
      pageSize: 10,
      total: 1
    };
  },
  computed: {
    filteredOptionalColumns() {
      return this.optionalColumns.filter(column => this.isColumnVisible(column.value));
    }
  },
  methods: {
    handleSearch(currentPage) {
      // 调用后端API进行查询
      if (!this.$request) {  
      console.error('this.$request is not defined');  
      return; // 或者你可以抛出一个错误，或者执行其他错误处理逻辑  
    }  
      if (currentPage) this.currentPage = currentPage
      this.$request.get('/info/search', { 
        params: { 
          query: this.searchQuery, 
          currentPage: this.currentPage, 
          size: this.pageSize } })
        .then(response => {
          this.tableData = response.data?.list;
          this.total = response.data?.total;
        })
        .catch(error => {
          this.$message.error('查询失败');
        });
    },
    handleDelete() {
      // 调用后端API删除选中的行
      const ids = this.selectedRows.map(row => row.id);
      this.$request.delete('/info/delete/batch', { data:ids })
        .then(response => {
          this.$message.success('删除成功');
          this.handleSearch(1); // 重新查询
        })
        .catch(error => {
          this.$message.error('删除失败');
        });
    },
    handleEdit(row) {
      this.editForm = { ...row };
      this.editDialogVisible = true;
      this.editId = row.id;
    },
    handleEditSubmit() {
      // 调用后端API提交修改
      this.$request.post('/info/edit/'+this.editId, this.editForm)
        .then(response => {
          this.$message.success('修改成功');
          this.editDialogVisible = false;
          this.handleSearch(1); // 重新查询
        })
        .catch(error => {
          this.$message.error('修改失败');
        });
    },
    handleColumnChange() {
      // 强制选择公司、岗位、官网链接、备注
      const mandatoryColumns = ['company', 'jobTitle', 'website', 'notes'];
      this.selectedColumns = [...new Set([...this.selectedColumns, ...mandatoryColumns])];
    },
    isColumnVisible(column) {
      return this.selectedColumns.includes(column);
    },
    showNotes(notes) {
      this.selectedNotes = notes;
      this.dialogVisible = true;
    },
    handleSelectionChange(selection) {
      this.selectedRows = selection;
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.handleSearch();
    },
    formatUrl(url) {
    if (!/^https?:\/\//i.test(url)) {
      return 'http://' + url;
    }
    return url;
    }
  },
  mounted() {
    this.handleSearch();
  }
};
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: auto;
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.search-container {
  display: flex;
  align-items: center;
}

.search-input {
  width: 500px;
  margin-right: 10px;
}

.custom-table .el-table__header-wrapper {
  background-color: #f5f7fa;
}
.custom-table .el-table__header th {
  background-color: #ebeef5;
  color: #13bed8ef;
  font-weight: bold;
  text-align: center;
}

.custom-table .el-table__body tr:hover {
  background-color: #f5f7fa;
}

.custom-table .el-table__body td {
  text-align: center;
}

.actions {
  margin-top: 20px;
}

.dialog-footer {
  text-align: right;
}
</style>


